package greedy_algorithm

func MaxCapacity(ht []int) int {
	var maxCap, i, j = 0, 0, len(ht) - 1
	minIndex := func(a, b int) int {
		if ht[a] < ht[b] {
			return a
		}
		return b
	}

	max := func(a, b int) int {
		if a < b {
			return b
		}
		return a
	}

	calIndexAndCap := func(x, y int) (int, int) {
		index := minIndex(ht[x], ht[y])
		return index, (y - x) * ht[index]
	}

	for i != j {
		index, c := calIndexAndCap(i, j)
		if index == i {
			i++
		} else {
			j--
		}
		maxCap = max(maxCap, c)
	}
	return maxCap
}
